/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tallerfinal.util.distributions.factory;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.TreeMap;
import tallerfinal.util.distributions.Generator;
import tallerfinal.util.enums.DistributionsName;

/**
 *
 * @author ricardo
 */
public class GeneratorsFactory {

    private static GeneratorsFactory instance;

    private GeneratorsFactory() {
    }
    private TreeMap<DistributionsName, Class> m_RegisteredProducts = new TreeMap<>();

    public void registerProduct(DistributionsName productID, Class productClass) {
        m_RegisteredProducts.put(productID, productClass);
    }

    public synchronized static GeneratorsFactory getInstance() {
        if (instance == null) {
            instance = new GeneratorsFactory();
        }
        return instance;
    }

    public Generator<?> createGenerator(DistributionsName generatorId, Object... objects) throws NoSuchMethodException, InstantiationException, InvocationTargetException, IllegalAccessException {
        Class<?> generatorClass = m_RegisteredProducts.get(generatorId);
        Constructor productConstructor = generatorClass.getConstructors()[0];
        return (Generator) productConstructor.newInstance(objects);

    }
}
